package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.McUserInfoMapper;
import cc.rengu.igas.momp.common.entity.TMMcUserInfo;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Jinan Liu
 * @date 2020/3/26 00:22
 * @description 营销活动机构规则数据库操作接口实现
 */
public class McUserInfoMapperImpl implements McUserInfoMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    /**
     * 营销活动用户信息列表查询接口实现
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMMcUserInfo>
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/3/26 00:24
     */
    @Override
    public List<TMMcUserInfo> selectMcUserInfoList(String instId, String mcNo) throws Exception {

        String queryClause = "INST_ID=[" + instId + "], MC_NO=[" + mcNo + "]";

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMcUserInfoList");

        // 按接口赋查询条件
        TMMcUserInfo mcUserInfo = new TMMcUserInfo();
        mcUserInfo.setInstId(instId);
        mcUserInfo.setMcNo(mcNo);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mcUserInfo, TMMcUserInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据查询条件<{}>获取营销活动用户信息列表失败!", queryClause);
            return null;
        }

        return objectList.stream().map(item -> (TMMcUserInfo) (item)).collect(Collectors.toList());
    }

    /**
     * 营销活动用户信息列表查询接口实现
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMMcUserInfo>
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/3/23 20:24
     */
    @Override
    public PageResult selectMcUserInfoList(String instId, String mcNo, PageInfo pageInfo) throws Exception {
        String queryClause = "INST_ID=[" + instId + "], MC_NO=[" + mcNo + "]";

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMcUserInfoList");

        // 按接口赋查询条件
        TMMcUserInfo mcUserInfo = new TMMcUserInfo();
        mcUserInfo.setInstId(instId);
        mcUserInfo.setMcNo(mcNo);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mcUserInfo, TMMcUserInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据查询条件<{}>获取营销活动用户信息列表失败!", queryClause);
            return null;
        }
        return dbsUtil.dbsFetchPageListData(sqlStr, mcUserInfo, TMMcUserInfo.class, pageInfo);
    }

    /**
     * 营销活动用户信息列表新增接口实现
     *
     * @param mcUserInfoList 营销活动用户信息表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/1 22:02
     */
    @Override
    public int insertMcUserInfoListByBank(List<TMMcUserInfo> mcUserInfoList) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMcUserInfoListByBank");

        for (TMMcUserInfo tmMcUserInfo : mcUserInfoList) {

            int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmMcUserInfo, TMMcUserInfo.class);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("插入法人机构号<{}>营销活动编号<{}>账号<{}>的营销活动用户信息失败!<{}>", tmMcUserInfo.getInstId(), tmMcUserInfo.getMcNo(), tmMcUserInfo.getAcctNo(), iReturnCode);
                return iReturnCode;
            }
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 营销活动用户信息列表修改接口实现
     *
     * @param mcUserInfoList
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/1 22:02
     */
    @Override
    public int updateMcUserInfoListByBank(List<TMMcUserInfo> mcUserInfoList) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMcUserInfoListByBank");

        for (TMMcUserInfo tmMcUserInfo : mcUserInfoList) {

            int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmMcUserInfo, TMMcUserInfo.class);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("更新法人机构号<{}>营销活动编号<{}>账号<{}>的营销活动用户信息失败!<{}>", tmMcUserInfo.getInstId(), tmMcUserInfo.getMcNo(), tmMcUserInfo.getAcctNo(), iReturnCode);
                return iReturnCode;
            }
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 营销活动用户信息列表删除接口实现
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/1 22:02
     */
    @Override
    public int deleteMcUserInfoListByBank(String instId, String mcNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "deleteMcUserInfoListByBank");

        TMMcUserInfo tmMcUserInfo = new TMMcUserInfo();
        tmMcUserInfo.setInstId(instId);
        tmMcUserInfo.setMcNo(mcNo);

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmMcUserInfo, TMMcUserInfo.class);
        if (Database.DBS_SUCCESS != iReturnCode && Database.DBS_NOT_FOUND != iReturnCode) {
            rglog.error("删除法人机构号<{}>营销活动编号<{}>的营销活动用户信息失败!<{}>", tmMcUserInfo.getInstId(), tmMcUserInfo.getMcNo(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 新增单条营销活动用户信息数据库操作实现
     *
     * @param instId     法人机构号
     * @param mcUserInfo
     * @return int
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/2 00:08
     */
    @Override
    public int insertMcUserInfo(String instId, TMMcUserInfo mcUserInfo) throws Exception {
        return 0;
    }

    /**
     * 更新单条营销活动用户信息数据库操作实现
     *
     * @param instId     法人机构号
     * @param mcUserInfo
     * @return int
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/2 00:08
     */
    @Override
    public int updateMcUserInfo(String instId, TMMcUserInfo mcUserInfo) throws Exception {
        return 0;
    }

    /**
     * 使用主键删除单条营销活动用户信息数据库操作实现
     *
     * @param instId   法人机构号
     * @param mcNo     营销活动编号
     * @param acctNo
     * @param acctType
     * @return int
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/2 00:08
     */
    @Override
    public int deleteMcUserInfoByPrimaryKey(String instId, String mcNo, String acctNo, String acctType) throws Exception {
        return 0;
    }


    /**
     * 使用主键查询单条营销活动用户信息数据库操作实现
     *
     * @param instId   法人机构号
     * @param mcNo     营销活动编号
     * @param acctNo
     * @param acctType
     * @return int
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/2 00:08
     */
    @Override
    public int selectMcUserInfoByPrimaryKey(String instId, String mcNo, String acctNo, String acctType) throws Exception {
        return 0;
    }

    /**
     * 使用法人机构号、营销活动编号和用户ID获取营销活动用户信息数据库操作实现
     * 查询成功返回0
     * 查询失败返回-1
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @param acctNo 账号
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/14 16:43
     */
    @Override
    public int selectSingleMcUserInfoByUserId(String instId, String mcNo, String acctNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectSingleMcUserInfoByUserId");

        TMMcUserInfo tmMcUserInfo = new TMMcUserInfo();
        tmMcUserInfo.setInstId(instId);
        tmMcUserInfo.setMcNo(mcNo);
        tmMcUserInfo.setAcctNo(acctNo);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmMcUserInfo, tmMcUserInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件法人机构号<{}>获取营销产品信息失败!<{}>", tmMcUserInfo.getInstId(), iReturnCode);
            return CommonConstant.PROCESS_FAILED;
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 营销活动用户信息列表查询接口实现
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @param acctNo 账号
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMMcUserInfo>
     * @throws Exception 异常信息
     * @author JL PANG
     * @date 2020/9/28 00:24
     */
    @Override
    public List<TMMcUserInfo> selectMcUserInfoListByInstIdMcNoAcctNo(String instId, String mcNo, String acctNo) throws Exception {

        String queryClause = "INST_ID=[" + instId + "], MC_NO=[" + mcNo + "], ACCTNO=[" + acctNo + "]";

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectSingleMcUserInfoByUserId");

        // 按接口赋查询条件
        TMMcUserInfo mcUserInfo = new TMMcUserInfo();
        mcUserInfo.setInstId(instId);
        mcUserInfo.setMcNo(mcNo);
        mcUserInfo.setAcctNo(acctNo);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mcUserInfo, TMMcUserInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据查询条件<{}>获取营销活动用户信息列表失败!", queryClause);
            return null;
        }

        return objectList.stream().map(item -> (TMMcUserInfo) (item)).collect(Collectors.toList());
    }
}
